<?php

/**
 * Lumad CMS
 * Copyright (c)	2007, Jason A. Banico
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright (c) 2007, Jason A. Banico
 * @link			http://cakeforge.org/projects/lumad-cms/
 * @package			lumad-cms
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class ContentPagesController extends AppController {

  var $name = 'ContentPages';
  var $components = array('context','RequestHandler');
  var $helpers = array('Html','Text','Time');

  function admin_index() {

    // Check if user is authorized to access this method.
    $this->context->checkSession($this);

    // Highlight the appropriate submenu item. Done through the context component.
    $this->context->method='index';

    // Make use of the admin.thtml template.
    $this->layout="admin";

    $contentpages=$this->ContentPage->findAll();

    if (!empty($contentpages)) {
      $this->set('content_pages', $contentpages);
    }
    else
    // Default to add a new page if list is empty.
    {
      $this->redirect('/content_pages/add');
    }
  }

  function admin_add() {
    $this->context->checkSession($this);
    $this->context->method='add';
    $this->layout="admin";

    if (!empty($this->data)) {
      // Take out next line characters in post.
      $this->data['ContentPage']['content']=str_replace(array("\r\n", "\r", "\n"), "", $this->data['ContentPage']['content']);
      if ($this->ContentPage->save($this->data)) {
        // Retrieve the latest page created and create a menu item for it.
        $content_page_id = $this->ContentPage->getLastInsertId();
        $this->data['MenuItem']['content_page_id'] = $content_page_id;
        $this->data['MenuItem']['name'] = $this->data['ContentPage']['title'];
        $this->data['MenuItem']['url'] = $this->data['ContentPage']['url'];
        $this->ContentPage->MenuItem->save($this->data);
        $this->redirect('/admin/content_pages/');
      }
    }
  }

  function admin_edit($id = null) {
    $this->context->checkSession($this);
    $this->context->method='index';
    $this->layout="admin";

    if (empty($this->data)) {
      $this->ContentPage->id = $id;
      $this->data = $this->ContentPage->read();
      $this->set('content_page', $this->ContentPage->read());
    }
    else
    {
      // Take out next line characters in post.
      $this->data['ContentPage']['content']=str_replace(array("\r\n", "\r", "\n"), "", $this->data['ContentPage']['content']);
      if ($this->ContentPage->save($this->data['ContentPage'])) {
        // Update the corresponding menu item as well.
        $this->data['MenuItem']['name'] = $this->data['ContentPage']['title'];
        $this->data['MenuItem']['url'] = $this->data['ContentPage']['url'];
        $this->ContentPage->MenuItem->save($this->data['MenuItem']);
        $this->redirect('/admin/content_pages/');
      }
    }
  }

  function admin_delete($id = null) {
    $this->context->checkSession($this);
    $this->context->method='index';
    $this->layout="admin";

    if (empty($this->data)) {
      $this->ContentPage->id = $id;
      $this->data = $this->ContentPage->read();
      $this->set('content_page', $this->ContentPage->read());
    }
    else
    {
      // Delete menu item first, then delete the page.
      $this->ContentPage->MenuItem->del($this->data['MenuItem']['id']);
      $this->ContentPage->del($this->data['ContentPage']['id']);
      $this->redirect('/admin/content_pages/');
    }
  }

  function display($id) {
    $this->ContentPage->id = $id;
    // Retrieve the menu items as well.
    $this->set('menuitems', $this->requestAction('/menu_items/getMenuItems'));
    $this->set('display', $this->ContentPage->read());
  }

  function displayurl($url) {
    $this->set('menuitems', $this->requestAction('/menu_items/getMenuItems'));
    // Return the /~ prefix to the URL, which was taken out by htaccess.
    $url = '/~'.$url;
    $this->set('display', $this->ContentPage->findByUrl($url));
  }

}
?>